package com.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class UploadController {

    @RequestMapping("/upload1")
    public String upload1(MultipartFile headImg, HttpServletRequest request){
        System.out.println(headImg);
        System.out.println(headImg.getName());//获取的是 表单中 文件域 的name属性的值
        System.out.println(headImg.getOriginalFilename());// 获取用户上传的真正文件的文件名
        System.out.println(headImg.getContentType());//获取文件的 mimeType
        System.out.println(headImg.getSize());//获取文件的 大小
        String realPath = request.getServletContext().getRealPath("/uploadFile");
        File dest = new File(realPath,headImg.getOriginalFilename());
//        File dest = new File("c:/imgs/"+headImg.getOriginalFilename());
        try {
            headImg.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/index.jsp";
    }

    @RequestMapping("/upload2")
    public String upload1(MultipartFile headImg, String username,String password,HttpServletRequest request){
        System.out.println(username);
        System.out.println(password);
        System.out.println(headImg.getName());//获取的是 表单中 文件域 的name属性的值
        System.out.println(headImg.getOriginalFilename());// 获取用户上传的真正文件的文件名
        System.out.println(headImg.getContentType());//获取文件的 mimeType
        System.out.println(headImg.getSize());//获取文件的 大小
        String realPath = request.getServletContext().getRealPath("/uploadFile");
        File dest = new File(realPath,headImg.getOriginalFilename());
//        File dest = new File("c:/imgs/"+headImg.getOriginalFilename());
        try {
            headImg.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/index.jsp";
    }
}
